package com.iseastar.guojiang.transfer;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.model.NewParcelBean;
import com.iseastar.guojiang.model.TransferTodayParcelBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.StringUtils;
import com.shouhuobao.take.weight.CircularImage;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterWithImage;
import droid.frame.utils.sqlite.DateUtils;

/**
 * Created by joker on 2018/1/30.
 * 今日揽收包裹适配器
 */
public class TransferTodayParcelListAdapter extends BaseAdapterWithImage<TransferTodayParcelBean> {

    public TransferTodayParcelListAdapter(ArrayList<TransferTodayParcelBean> items, Activity context, ViewGroup listView) {
        super(items, context, listView);
        setEmptyView(R.layout.layout_transfer_today_empty);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.transfer_today_parcel_item, parent, false);
            holder = new ViewHolder();
            holder.courierHeadIV = (CircularImage) convertView.findViewById(R.id.courier_head_iv);
            holder.parcelParentLayout = (LinearLayout) convertView.findViewById(R.id.parcel_parent_layout);
            holder.courierNameTV = (TextView) convertView.findViewById(R.id.courier_name_tv);
            holder.courierCardTV = (TextView) convertView.findViewById(R.id.courier_card_tv);
            holder.courierPhoneIV = (ImageView) convertView.findViewById(R.id.courier_phone_iv);
            holder.parcelDescTV = (TextView) convertView.findViewById(R.id.desc_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final TransferTodayParcelBean item = getItem(position);
        showImage(item.getImagePath(), holder.courierHeadIV, null);
        holder.courierNameTV.setText(item.getCourierNickName() + "（" + item.getCourierName() + "）");
        holder.courierCardTV.setText(item.getCourierCard());
        holder.parcelDescTV.setText(DateUtils.format(item.getCreateTime()).substring(11) + " 共收到该火箭侠" + item.getParcelCnt() + "个包裹");
        holder.courierPhoneIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intents.dialOrCall(item.getCourierPhone());
            }
        });
        holder.parcelParentLayout.removeAllViews();
        if (item.getParcelDtos().size() > 0) {
            for (int i = 0; i < item.getParcelDtos().size(); i++) {
                final NewParcelBean parcelBean = item.getParcelDtos().get(i);
                View parcelItem = LayoutInflater.from(context).inflate(R.layout.transfer_today_parcel_item1, null);
                ImageView parcelHeadIV = (ImageView) parcelItem.findViewById(R.id.parcel_head_iv);
                TextView phoneTV = (TextView) parcelItem.findViewById(R.id.order_phone_tv);
                TextView bagCodeTV = (TextView) parcelItem.findViewById(R.id.bag_code_tv);
                TextView orderTimeTV = (TextView) parcelItem.findViewById(R.id.order_time_tv);
                View bottomLine = parcelItem.findViewById(R.id.bootom_line);
                showImage(parcelBean.getParcelImgUrl(), parcelHeadIV, null);
                parcelHeadIV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!StringUtils.isEmpty(parcelBean.getParcelImgUrl())) {
                            DialogMgr.showLargeStationPhoto(context, parcelBean.getParcelImgUrl());
                        }
                    }
                });
                phoneTV.setText("下单人手机号：" + parcelBean.getUserPhone());
                orderTimeTV.setText("下单时间：" + DateUtils.format(parcelBean.getCreateTime()));
                if (StringUtils.isEmpty(parcelBean.getBagCode())) {
                    bagCodeTV.setText("投递编号：无");
                } else {
                    bagCodeTV.setText("投递编号：" + parcelBean.getBagCode());
                }
                if (i == item.getParcelDtos().size() - 1) {
                    bottomLine.setVisibility(View.GONE);
                } else {
                    bottomLine.setVisibility(View.VISIBLE);
                }
                holder.parcelParentLayout.addView(parcelItem);
            }
        }
        return convertView;
    }

    class ViewHolder {
        LinearLayout parcelParentLayout;
        CircularImage courierHeadIV;//火箭侠头像
        TextView courierNameTV;//火箭侠花名+真实姓名
        TextView courierCardTV;//火箭侠身份证号
        ImageView courierPhoneIV;//拨打火箭侠电话
        TextView parcelDescTV;//包裹描述
    }

}
